package ee.fujitsu.entity.state;
import java.io.Console;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import ee.fujitsu.common.Printer;
import ee.fujitsu.common.Type;
import ee.fujitsu.entity.Customer;
import ee.fujitsu.entity.JournalRecord;
import ee.fujitsu.entity.Movie;
import ee.fujitsu.factory.TypeFactory;

/**
 * possible Film state changes implementation for films in store.
 */

/**
 * @author Andrei
 *
 * 10.09.2012	
 */
public class StateInStore implements State{
	
	/** The console. */
	Console console = System.console();

	  /**
  	 * Delete film from store and put in into deleted list.
  	 *
  	 * @param moviesStore the movies store
  	 * @param moviesDeleted the movies deleted
  	 * @param index the index
  	 * @return true, if successful
  	 */
	public boolean deleteFilm(Map<Integer, Movie> moviesStore, Map<Integer, Movie> moviesDeleted, int index) {

		  	Movie movie = moviesStore.get(index);
			if (movie != null) {
				Printer.print("Delete id - " + index + " Film title -"
						+ movie.getTitle());
			  moviesDeleted.put(index, movie);
			  moviesStore.remove(index);
			  return true;
			} else {
				Printer.print("Cant find id: " + index);
				return false;
			}
		
	}

	  /**
  	 * Add film to store.
  	 *
  	 * @param movies the movies
  	 * @return true, if successful
  	 */
	@SuppressWarnings("unused")
	public boolean addFilm(Map<Integer, Movie> movies) {
	    String title = console.readLine("Film title? ");
	    String type = getTypeConsole("Film type?");
	    if (type.equals("quit")) return false;
		Printer.print(title + " " + type);

	      List<Integer> movieIndexes = new ArrayList<Integer>(movies.keySet());
	      Collections.sort(movieIndexes, Collections.reverseOrder());
		  Movie movie = new Movie(title, type);
		  int key = movieIndexes.get(0)+1;

		  if (movie != null) {
			  	movies.put(key, movie);
		  		return true;
		  }
		return false;
		  
	}
	  
  	/**
  	 * Rent film from store.
  	 *
  	 * @param moviesStore the movies store
  	 * @param moviesRent the movies rent
  	 * @param startDate the start date
  	 * @param expectedEnd the expected end
  	 * @param realEnd the real end
  	 * @param client the client
  	 * @param bonus the bonus
  	 * @return the journal record
  	 */
	public JournalRecord rentFilm(Map<Integer, Movie> moviesStore,
			Map<Integer, Movie> moviesRent,
			Date startDate,
			Date expectedEnd,
			Date realEnd,
			Customer client, boolean bonus) {
	      List<Integer> movieIndexes = new ArrayList<Integer>(moviesStore.keySet());
		  int mind = movieIndexes.get(new Random().nextInt(movieIndexes.size()-1));

		  Movie movie = moviesStore.get(mind);
		  TypeFactory factory = new TypeFactory();
		  JournalRecord record = null;
		  if (!bonus)
			  record = factory.getInitialRecord(movie, startDate, expectedEnd, realEnd, client);
		  else
			  record = factory.getRegularRecord(movie, startDate, expectedEnd, realEnd, client);
		  moviesRent.put(mind, movie);
		  moviesStore.remove(mind);
		  return record;
		
	}
	  
  	/**
  	 * Return film to store.
  	 *
  	 * @param moviesStore the movies store
  	 * @param moviesRent the movies rent
  	 */

	public void returnFilm(Map<Integer, Movie> moviesStore, Map<Integer, Movie> moviesRent) {
		
	}
	  
  	/**
  	 * Return film to store.
  	 *
  	 * @param movies the movies
  	 * @param index the index
  	 * @return true, if successful
  	 */

	public boolean changeFilm(Map<Integer, Movie> movies, int index ) {
		Movie movie = movies.get(index);
		if (movie != null) {
			Printer.print("id - " + index + " Film title -"
					+ movie.getTitle() + " type is - "+ movie.getType());
		    String newType = getTypeConsole("New film type?");
		    if (newType.equals("quit")) return false;
			movie.setType(newType);
			Printer.print("Films quantity: " + movies.size());
			Printer.print("id - " + index + " Film title -"
					+ movie.getTitle() + " type is - "+ movie.getType());
			return true;
		} else {
			Printer.print("Cant find id: " + index);
		}
		return false;
	}
	  
  	/**
  	 * private method for console interact.
  	 *
  	 * @param title the title
  	 * @return the type console
  	 */
	private String getTypeConsole(String title) {
		String type = console.readLine(title
				+ " (new, old(released before 1980y), regular) ");

		if (!(type.equals(Type.NEW_RELEASE.getType())
				|| type.equals(Type.OLD_FILM.getType()) || type
					.equals(Type.REGULAR_FILM.getType()))) {
			Printer.print("Film type not correct");
			while (!(type.equals(Type.NEW_RELEASE.getType())
					|| type.equals(Type.OLD_FILM.getType())
					|| type.equals(Type.REGULAR_FILM.getType()) || type
						.equals("quit")))
				type = console
						.readLine(title
								+ " (new, old(released before 1980y), regular) or (type 'quit' to exit)");

		}
		return type;
	}

}
